from typing import List


class Solution:
    def isEscapePossible(self, blocked: List[List[int]], source: List[int],
                         target: List[int]) -> bool:
        n = len(blocked)
        nc = (n - 1) * n // 2 + 2
        count = 0
        blocked = set([tuple(b) for b in blocked])
        visited = set()

        def bfs1(i, j, test):
            if i < 0 or j < 0 or i >= 10**6 or j >= 10**6:
                return 0
            nonlocal count, visited

            if (i, j) in blocked or (i, j) in visited:
                return 0

            if i == test[0] and j == test[1]:
                return 2
            count += 1
            visited.add((i, j))

            if count < nc and bfs1(i - 1, j, test) == 2:
                return 2

            if count < nc and bfs1(i + 1, j, test) == 2:
                return 2

            if count < nc and bfs1(i, j - 1, test) == 2:
                return 2

            if count < nc and bfs1(i, j + 1, test) == 2:
                return 2

            if count >= nc:
                return 1

            return 0

        tmp = bfs1(source[0], source[1], target)

        if tmp == 2:
            return True

        if tmp == 0:
            return False
        count = 0
        visited = set()

        if bfs1(target[0], target[1], source) == 0:
            return False

        return True


blocked = [[0, 1], [1, 0]]
source = [0, 0]
target = [0, 2]
blocked = []
source = [0, 0]
target = [999999, 999999]
blocked = [[100059, 100063], [100060, 100064], [100061, 100065],
           [100062, 100066], [100063, 100067], [100064, 100068],
           [100065, 100069], [100066, 100070], [100067, 100071],
           [100068, 100072], [100069, 100073], [100070, 100074],
           [100071, 100075], [100072, 100076], [100073, 100077],
           [100074, 100078], [100075, 100079], [100076, 100080],
           [100077, 100081], [100078, 100082], [100079, 100083],
           [100080, 100082], [100081, 100081], [100082, 100080],
           [100083, 100079], [100084, 100078], [100085, 100077],
           [100086, 100076], [100087, 100075], [100088, 100074],
           [100089, 100073], [100090, 100072], [100091, 100071],
           [100092, 100070], [100093, 100069], [100094, 100068],
           [100095, 100067], [100096, 100066], [100097, 100065],
           [100098, 100064], [100099, 100063], [100098, 100062],
           [100097, 100061], [100096, 100060], [100095, 100059],
           [100094, 100058], [100093, 100057], [100092, 100056],
           [100091, 100055], [100090, 100054], [100089, 100053],
           [100088, 100052], [100087, 100051], [100086, 100050],
           [100085, 100049], [100084, 100048], [100083, 100047],
           [100082, 100046], [100081, 100045], [100080, 100044],
           [100079, 100043], [100078, 100044], [100077, 100045],
           [100076, 100046], [100075, 100047], [100074, 100048],
           [100073, 100049], [100072, 100050], [100071, 100051],
           [100070, 100052], [100069, 100053], [100068, 100054],
           [100067, 100055], [100066, 100056], [100065, 100057],
           [100064, 100058], [100063, 100059], [100062, 100060],
           [100061, 100061], [100060, 100062]]
source = [100079, 100063]
target = [999948, 999967]
print(Solution().isEscapePossible(blocked, source, target))
